//
//  JBUnitGridView.m
//  JBCalendar
//
//  Created by YongbinZhang on 7/8/13.
//  Copyright (c) 2013 JustBen. All rights reserved.
//

#import "JBUnitGridView.h"


#ifndef TileSize
#define TileSize
#define TileSize_W          46.0f
#define TileSize_Month_H    39.0f
#define TileSize_Week_H     60.0f
#endif

#ifndef TileCountInOneLine
#define TileCountInOneLine  7
#endif

#ifndef MaxTileRowInUnit
#define MaxTileRowInUnit
#define MaxTileRowInUnit_Month    6
#define MaxTileRowInUnit_Week     1
#endif


@interface JBUnitGridView ()

//  月视图 or 周视图
@property (nonatomic, assign, readwrite) UnitType unitType;

//  月视图中的所有unitTileView
@property (nonatomic, retain) NSMutableArray *monthTileViews;
//  周视图中的所有unitTileView
@property (nonatomic, retain) NSMutableArray *weekTileViews;

//  月视图中tile的数量
@property (nonatomic, assign) NSInteger monthTilesCount;
//  周视图中tile的数量
@property (nonatomic, assign) NSInteger weekTilesCount;
/**************************************************************
 *@Description:重新加载Tiles
 *@Params:nil
 *@Return:nil
 **************************************************************/
- (void)reloadTiles;

@end


@implementation JBUnitGridView


#pragma mark -
#pragma mark - init
/**************************************************************
 *@Description:初始化方法
 *@Params:nil
 *@Return:nil
 **************************************************************/
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setUserInteractionEnabled:YES];
        
        self.monthTileViews = [[NSMutableArray alloc] init];
        self.weekTileViews = [[NSMutableArray alloc] init];
        
        self.unitTileViewWidth = TileSize_W;
        
        self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.backgroundColor = [UIColor clearColor];
        [self addSubview:self.backgroundView];
    }
    return self;
}

/**************************************************************
 *@Description:初始化方法
 *@Params:
 *  frame:框架
 *  unitType:月视图／周视图
 *  selectedDate:初始的选择日期
 *@Return:JBUnitGridView对象
 **************************************************************/
- (id)initWithFrame:(CGRect)frame UnitType:(UnitType)unitType SelectedDate:(NSDate *)selectedDate
{
    self = [self initWithFrame:frame];
    if (self) {
        self.unitType = unitType;
        switch (self.unitType) {  
            case UnitTypeMonth:{
                break;
            }
                
            case UnitTypeWeek:{
                break;
            }
                
            default:{
                self.unitType = UnitTypeMonth;
                break;
            }
        }
        
        if (self.unitType == UnitTypeMonth) {
            self.unitTileViewHeight = TileSize_Month_H;
            for (NSInteger i = 0; i < MaxTileRowInUnit_Month; i++) {
                for (NSInteger j = 0; j < TileCountInOneLine; j++) {
                    UnitTilePosition position;
                    position.line = j;
                    position.row = i;
                    
                    JBUnitTileView *tileView = [[JBUnitTileView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.unitTileViewWidth, self.unitTileViewHeight) Position:position];
                    [self.monthTileViews addObject:tileView];
                }
            }
        } else if (self.unitType == UnitTypeWeek){
            self.unitTileViewHeight = TileSize_Week_H;
            for (NSInteger i = 0; i < MaxTileRowInUnit_Week; i++) {
                for (NSInteger j = 0; j < TileCountInOneLine; j++) {
                    UnitTilePosition position;
                    position.line = j;
                    position.row = i;
                    
                    JBUnitTileView *tileView = [[JBUnitTileView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.unitTileViewWidth, self.unitTileViewHeight) Position:position];
                    [self.weekTileViews addObject:tileView];
                    [self addSubview:tileView];
                }
            }
        }
        
        if (selectedDate) {
            self.selectedDate = selectedDate;
        } else {
            self.selectedDate = [NSDate date];
        }
    }
    
    return self;
}


#pragma mark -
#pragma mark - Class Extensions
/**************************************************************
 *@Description:重新加载Tiles
 *@Params:nil
 *@Return:nil
 **************************************************************/
- (void)reloadTiles
{
    switch (self.unitType) {
        case UnitTypeMonth:{
            self.monthTilesCount = 0;
            
            [[JBCalendarLogic defaultCalendarLogic] moveToMonthForDate:self.selectedDate WithCompletionBlock:^(NSArray *daysInFinalWeekOfPreviousMonth, NSArray *daysInSelectedMonth, NSArray *daysInFirstWeekOfFollowingMonth) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (daysInFinalWeekOfPreviousMonth && daysInFinalWeekOfPreviousMonth.count > 0) {
                        for (JBCalendarDate *date in daysInFinalWeekOfPreviousMonth) {
                            JBUnitTileView *tileView = [self.monthTileViews objectAtIndex:self.monthTilesCount];
                            tileView.date = date;
                            self.monthTilesCount++;

                            if (!tileView.superview) {
                                [self addSubview:tileView];
                            }
                        }
                    }
                    
                    if (daysInSelectedMonth && daysInSelectedMonth.count > 0) {
                        for (JBCalendarDate *date in daysInSelectedMonth) {
                            JBUnitTileView *tileView = [self.monthTileViews objectAtIndex:self.monthTilesCount];
                            tileView.date = date;
                            self.monthTilesCount++;
                            
                            if (!tileView.superview) {
                                [self addSubview:tileView];
                            }
                        }
                    }
                    
                    if (daysInFirstWeekOfFollowingMonth && daysInFirstWeekOfFollowingMonth.count > 0) {
                        for (JBCalendarDate *date in daysInFirstWeekOfFollowingMonth) {
                            JBUnitTileView *tileView = [self.monthTileViews objectAtIndex:self.monthTilesCount];
                            tileView.date = date;
                            self.monthTilesCount++;
                            
                            if (!tileView.superview) {
                                [self addSubview:tileView];
                            }
                            
                        }
                    }
                    
                    for (NSInteger i = self.monthTilesCount; i < self.monthTileViews.count; i++) {
                        JBUnitTileView *tileView = [self.monthTileViews objectAtIndex:i];
                        [tileView removeFromSuperview];
                    }
                    
                    CGRect tmpFrame = self.frame;
                    tmpFrame.size.height = (self.monthTilesCount / TileCountInOneLine) * self.unitTileViewHeight;
                    self.frame = tmpFrame;
                });
            }];
            
            
            break;
        }
            
        case UnitTypeWeek:{
            self.weekTilesCount = 0;
            
            [[JBCalendarLogic defaultCalendarLogic] moveToWeekForDate:self.selectedDate WithCompletionBlock:^(NSArray *daysInSelectedWeekInPreviousMonth, NSArray *daysInSelectedWeekInSelectedMonth, NSArray *daysInSelectedWeekInFollowingMonth) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (daysInSelectedWeekInPreviousMonth && daysInSelectedWeekInPreviousMonth.count > 0) {
                        for (JBCalendarDate *date in daysInSelectedWeekInPreviousMonth) {
                            JBUnitTileView *tileView = [self.weekTileViews objectAtIndex:self.weekTilesCount];
                            tileView.date = date;
                            self.weekTilesCount++;
                        }
                    }
                    
                    if (daysInSelectedWeekInSelectedMonth && daysInSelectedWeekInSelectedMonth.count > 0) {
                        for (JBCalendarDate *date in daysInSelectedWeekInSelectedMonth) {
                            JBUnitTileView *tileView = [self.weekTileViews objectAtIndex:self.weekTilesCount];
                            tileView.date = date;
                            self.weekTilesCount++;
                        }
                    }
                    
                    if (daysInSelectedWeekInFollowingMonth && daysInSelectedWeekInFollowingMonth.count > 0) {
                        for (JBCalendarDate *date in daysInSelectedWeekInFollowingMonth) {
                            JBUnitTileView *tileView = [self.weekTileViews objectAtIndex:self.weekTilesCount];
                            tileView.date = date;
                            self.weekTilesCount++;
                        }
                    }
                });
            }];
            
            break;
        }
            
        default:
            break;
    }
}


#pragma mark -
#pragma mark - Object Methods
/**************************************************************
 *@Description:重新加载数据以显示界面
 *@Params:nil
 *@Return:nil
 **************************************************************/
- (void)reloadData
{

}


#pragma mark -
#pragma mark - Settors
- (void)setSelectedDate:(NSDate *)selectedDate
{
    if (!_selectedDate) {
        _selectedDate = selectedDate;
        [self reloadTiles];
    } else {
        BOOL needReload = NO;
        if (![selectedDate isEqualToDate:_selectedDate]) {
            switch (self.unitType) {
                case UnitTypeMonth:{
                    if (![selectedDate sameMonthWithDate:_selectedDate]) {
                        needReload = YES;
                    }
                    break;
                }
                
                case UnitTypeWeek:{
                    if (![selectedDate sameWeekWithDate:_selectedDate]) {
                        needReload = YES;
                    }
                    break;
                }
                default:
                    break;
            }
            
            _selectedDate = selectedDate;
            
            [self reloadTiles];
        }
    }
}

@end
